// import 'package:euromillions/tools/EJ_config.dart';
// import 'package:euromillions/tools/models/EJ_DataViewModels.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

import '../../tools/KaL_Config.dart';
import '../../tools/models/KaL_DataViewModels.dart';
// import 'package:normal_lotto/tools/NL_viewModel.dart';

// import '../../tools/NL_config.dart';

class EJChooseNumber extends StatefulWidget {
  EJChooseNumber({super.key, required this.callback});
  Function(List) callback;
  @override
  State<EJChooseNumber> createState() => _EJChooseNumberState();
}

class _EJChooseNumberState extends State<EJChooseNumber> {
  // late final Function(List) callback;

  late List<Widget> _rowViews = [];
  late List _numberDatas = [];
  late List<bool> _isSelected = [];
  late List _choosedRegularNumbers = []; //选中的regular
  late List _choosedExtranNumbers = []; //选中的extran
  late List _callBackNumbers = [];

  late List _regularChooses = []; //regularNum下标
  late List _extraChooses = [];

  _configData() {
    for (var element in _numberDatas) {
      _isSelected.add(false);
    }
    setState(() {});
  }

  _setSelectedNumber(int index) {
    _callBackNumbers.clear();
    // var ball = _numberDatas[index];
    // if (_callBackNumbers.contains(ball)) {
    //   _callBackNumbers.remove(ball);
    // } else {
    //   _callBackNumbers.add(ball);
    // }
    _choosedRegularNumbers.sort((a, b) => a.compareTo(b));
    for (var cNum in _choosedRegularNumbers) {
      // var number = int.parse(element['number']);
      for (var i = 0; i < KaL_Config.regularNumMax; i++) {
        var element = _numberDatas[i];
        var number = int.parse(element['number']);
        if (cNum == number) {
          _callBackNumbers.add(element);
        }
      }
    }
    // _callBackNumbers.addAll(_choosedExtranNumbers);
    _choosedExtranNumbers.sort((a, b) => a.compareTo(b)); //从小到大排序
    for (var cNum in _choosedExtranNumbers) {
      for (var i = KaL_Config.regularNumMax; i < _numberDatas.length; i++) {
        var element = _numberDatas[i];
        var number = int.parse(element['number']);
        if (cNum == number) {
          _callBackNumbers.add(element);
        }
      }
    }

    widget.callback(_callBackNumbers);
  }

  // 数组排序，将不是regularnumber排到最后
  sortNumbers(List numbers) {
    numbers.sort((a, b) {
      if (a['regularnumber'] == 0 && b['regularnumber'] == 1) {
        return 1;
      } else if (a['regularnumber'] == 1 && b['regularnumber'] == 0) {
        return -1;
      } else {
        return 0;
      }
    });
    return numbers;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _numberDatas = KaL_DataViewModels().EJ_ChooseNumbers;
    _configData();
  }

  @override
  Widget build(BuildContext context) {
    // return Row(
    //   children: _rowViews,
    // );
    return Row(
      children: List.generate(_numberDatas.length, (index) {
        var element = _numberDatas[index];
        var number = element['number'];
        var isRightNum = element['isRightNum'] != 0;
        var isRegular = element['regularnumber'] != 0;
        var hasborder = element['hasBorder'] > 0 ? true : false;
        return numberView(index, number, hasborder, isRightNum, isRegular);
      }),
    );
  }

  //格子+数字:hasBorder为是否有⭕️
  Widget numberView(
    int index,
    String num,
    bool hasBorder, [
    bool? isRightNum = false,
    bool? isRegular = true,
  ]) {
    double _viewH = 100;
    double _viewW = 25.0;
    double _topOffset = 10.0;
    if (_regularChooses.contains(index)) {
      isRightNum = true;
    } else if (_extraChooses.contains(index)) {
      isRightNum = true;
    }
    return GestureDetector(
      onTap: () {
        var element = _numberDatas[index];
        debugPrint('选中---$element');
        var number = int.parse(element['number']);
        bool isRedball = element['redBall'] > 0 ? true : false;
        if (isRedball) {
          //常号
          if (!_regularChooses.contains(index)) {
            if (_regularChooses.length >= KaL_Config.regularNumCount) {
              //常号数量为5
              return;
            }
            debugPrint('选中常号,下标——————$index');
            _regularChooses.add(index);
            _choosedRegularNumbers.add(number);
          } else {
            _regularChooses.remove(index);
            _choosedRegularNumbers.remove(number);
          }
        } else {
          //特号
          if (!_extraChooses.contains(index)) {
            // if (_extraChooses.isNotEmpty) {
            //   return;
            // }
            if (_extraChooses.length >= KaL_Config.extramNumCount) {
              //特号数量为2
              return;
            }
            debugPrint('选中特号,下标——————$index');
            _extraChooses.add(index);
            _choosedExtranNumbers.add(number);
          } else {
            _extraChooses.remove(index);
            _choosedExtranNumbers.remove(number);
          }
        }
        _setSelectedNumber(index);
        setState(() {});
      },
      child: Container(
        alignment: Alignment.topCenter,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(color: HexColor('#dddddd'), width: 1),
                right: BorderSide(color: HexColor('#dddddd'), width: 1))),
        width: _viewW,
        // height: _cellHeight - 10,
        height: _viewH,
        child: Padding(
          padding: EdgeInsets.only(top: _topOffset),
          child: number(num, hasBorder, isRightNum!, isRegular!),
        ),
      ),
    );
  }

  //数字
  Widget number(
    String num,
    bool hasBorder,
    bool isRightNum,
    bool isRegular,
  ) {
    double WH = 20;
    Color textColor = hasBorder ? KaL_Config.KaL_MainColor : Colors.black;
    double textSize = hasBorder ? 10 : 12;
    FontWeight weight = hasBorder ? FontWeight.w600 : FontWeight.w400;
    Color borderColor = KaL_Config.KaL_MainColor;
    Color bgColor = Colors.white;
    if (hasBorder) {
      if (isRegular) {
        bgColor = isRightNum ? KaL_Config.KaL_MainColor : bgColor;
        textColor = isRightNum ? Colors.white : KaL_Config.KaL_MainColor;
      } else {
        bgColor = isRightNum ? KaL_Config.KaL_MainColor : bgColor;
        borderColor = KaL_Config.KaL_MainColor;
        textColor = isRightNum ? Colors.white : KaL_Config.KaL_MainColor;
      }
    }

    return Container(
      width: WH,
      height: WH,
      alignment: Alignment.center,
      decoration: hasBorder
          ? BoxDecoration(
              color: bgColor,
              border: Border.all(width: 1, color: borderColor),
              borderRadius: BorderRadius.all(Radius.circular(WH * 0.5)),
            )
          : null,
      child: Text(
        num,
        style: TextStyle(
          fontWeight: weight,
          fontSize: textSize,
          color: textColor,
        ),
      ),
    );
  }
}
